package com.example.mbl.service.impl;

import com.example.mbl.dao.GalleryMapper;
import com.example.mbl.entity.Gallery;
import com.example.mbl.result.PageResult;
import com.example.mbl.result.WebRestResult;
import com.example.mbl.result.business.GalleryPageResult;
import com.example.mbl.service.GalleryService;
import com.example.mbl.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GalleryServiceImpl implements GalleryService {
    @Autowired
    private GalleryMapper galleryMapper;

    @Override
    public WebRestResult addGallery(Gallery gallery) {
        WebRestResult result = new WebRestResult();
        gallery.setId(UUIDUtil.randomUUID());
        if(galleryMapper.insert(gallery)==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setMessage("新增相片失败");
        }
        return result;
    }

    @Override
    public WebRestResult updateGallery(Gallery gallery) {
        WebRestResult result = new WebRestResult();
        if(galleryMapper.updateByPrimaryKeySelective(gallery)==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setMessage("修改相片失败");
        }
        return result;
    }

    @Override
    public WebRestResult deleteGallery(Gallery gallery) {
        WebRestResult result = new WebRestResult();
        if(galleryMapper.deleteByPrimaryKey(gallery.getId())==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setMessage("删除相片失败");
        }
        return result;
    }

    @Override
    public PageResult<GalleryPageResult> getGalleryList(String date, int page, int pagesize) {
        PageResult<GalleryPageResult> result = new PageResult<>();
        int start = (page-1) * pagesize;

        List<GalleryPageResult> list = galleryMapper.getGalleryList(date,start,pagesize);
        if(list.size()>0){
            result.setTotal(list.get(0).getCnt());
            result.setPageCount(list.get(0).getPage());
            result.setRows(list);
        }
        result.setResult(WebRestResult.SUCCESS);
        return result;
    }
}
